package br3akr.DeadlyDinnerParty.Server.Actions;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TimerTask;

import br3akr.DeadlyDinnerParty.Server.Engine.Session;
import br3akr.DeadlyDinnerParty.Server.Exceptions.DeadlyDinnerPartyException;
import br3akr.DeadlyDinnerParty.Server.World.Area;
import br3akr.DeadlyDinnerParty.Server.World.GameCharacter;
import br3akr.DeadlyDinnerParty.Server.World.Ghost;

public class RevealAction extends Action {
	boolean actionComplete;
    private ActionResponse actionResponse;
	TimerTask task = null;
	
    public static final Set<String> keywords;
	static
	{
		Set<String>tempKeywords = new HashSet<String>();
		tempKeywords.add("Reveal");
		tempKeywords.add("Unreveal");
		keywords = Collections.unmodifiableSet(tempKeywords);
	}
    
    public RevealAction() {
        super("Reveal", false);
    }
    
    public ActionResponse doAction(String action, Session session) throws DeadlyDinnerPartyException {
		if (action == null) {
			actionComplete = true;
			throw new DeadlyDinnerPartyException ("No action found.");
		}
		if (session == null) {
			actionComplete = true;
			throw new DeadlyDinnerPartyException ("No session found.");
		}
		
    	StringTokenizer tokenizedAction = new StringTokenizer(action);
    	String param1 = null;
    	
    	if (tokenizedAction.hasMoreTokens()) {
    		param1 = tokenizedAction.nextToken();
    	}
    	
    	param1 = action.substring(action.indexOf(param1) + param1.length()).trim();
    	
		GameCharacter actor = session.getCharacter();
		
		if (actor == null) {
			actionComplete = true;
			throw new DeadlyDinnerPartyException ("No character found for your session.");
		}
		
		if (!(actor instanceof Ghost)) {
			actionComplete = true;
			throw new DeadlyDinnerPartyException ("You must be a ghost in order to be revealed.");
		}
    	
		Ghost ghost = (Ghost)actor;
    	actionResponse = new ActionResponse();
    	actionResponse.setActingCharacter(ghost);
    	Area location = ghost.getLocation();
    	actionResponse.addAffectedArea(location);
    	if (action.equalsIgnoreCase("Reveal")) {
    		if (ghost.isRevealed()) {
    			actionComplete = true;
    			throw new DeadlyDinnerPartyException ("You are already revealed.");
    		}
	    	ghost.setRevealed(true);
	    	ghost.stopHiding();
	    	Set<GameCharacter> localOnlookers = location.getAreaOnlookers(ghost, null);
			actionResponse.addOnlookers(localOnlookers, "The air sucks out of the room and suddenly the ghost of " + ghost.getName() + " appears!");
			actionResponse.setActingCharacterDescription("You appear to the mortals around you!");
    	}
    	if (action.equalsIgnoreCase("Unreveal")) {
    		if (!ghost.isRevealed()) {
    			actionComplete = true;
    			throw new DeadlyDinnerPartyException ("You are not revealed.");
    		}
    		Set<GameCharacter> localOnlookers = location.getAreaOnlookers(ghost, null);
	    	ghost.setRevealed(false);
			actionResponse.addOnlookers(localOnlookers, "The air sucks out of the room and suddenly the ghost of " + ghost.getName() + " disappears!");
			actionResponse.setActingCharacterDescription("You dissapear from the site of the mortals around you!");
    	}
		
    	actionComplete = true;
        return actionResponse;
    }
	
    public ActionResponse getActionResponse() {
		return actionResponse;
	}
    
    public Set<String> getKeywords() {
        return keywords;
    }
}